package com.fire.control.system.module.dailyCheck.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.DailyCheckResultDTO;
import com.fire.control.system.entity.File;
import com.fire.control.system.module.dailyCheck.dao.DailyCheckMapper;
import com.fire.control.system.module.dailyCheck.service.IDailyCheckService;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.dailyCheck.DailyCheckRequestProtocol;
import com.fire.control.system.support.util.ResultPageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @ClassName: DailyCheckServiceImpl
 * @Description: TODO(日常巡检)
 * @author A18ccms a18ccms_gmail_com
 * @date 2017年7月5日 上午10:29:50
 */
@Service
public class DailyCheckServiceImpl implements IDailyCheckService {

	@Autowired
	private ProtocolSelector protocolSelector;

	@Autowired
	private DailyCheckMapper dailyCheckMapper;

	private ResultPageUtil resultPageUtil;
	/**
	 * <p>
	 * Title: getDailyCheckList
	 * </p>
	 * <p>
	 * Description: 查看日常巡检
	 * </p>
	 * 
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.dailyCheck.service.IDailyCheckService#getDailyCheckList(com.fire.control.system.support.protocol.dailyCheck.DailyCheckRequestProtocol)
	 */
	@Override
	public ResponseProtocol getDailyCheckList(DailyCheckRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		PageHelper.startPage(requestProtocol.getPage(), requestProtocol.getSize());
		List<DailyCheckResultDTO> dailyCheckList = dailyCheckMapper.getDailyCheckList(requestProtocol);
		Map<String,Object> returnMap = resultPageUtil.resultPage(new PageInfo<DailyCheckResultDTO>(dailyCheckList), dailyCheckList);
		if (!returnMap.isEmpty()) {
			response.setData(returnMap);
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("数据返回成功!");
		} else {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("数据返回失败!");
		}
		return response;
	}

	/**
	 * <p>
	 * Title: getDailyCheckDetail
	 * </p>
	 * <p>
	 * Description: 查看日常巡检详细
	 * </p>
	 * 
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.dailyCheck.service.IDailyCheckService#getDailyCheckDetail(com.fire.control.system.support.protocol.dailyCheck.DailyCheckRequestProtocol)
	 */
	@Override
	public ResponseProtocol getDailyCheckDetail(DailyCheckRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		ResponseProtocol response = protocolSelector.choose(requestProtocol);

		DailyCheckResultDTO dailyCheckDetail = dailyCheckMapper.getDailyCheckDetail(requestProtocol);

		List<File> fileList = dailyCheckMapper.getFileList(dailyCheckDetail.getId());
		String[] accessAdress = new String[fileList.size()];

		for (int i = 0; i < fileList.size(); i++) {
			accessAdress[i] = fileList.get(i).getAccessAddress();
		}
		
		if(accessAdress.length>0){
			accessAdress = dailyCheckDetail.getAccessAddress().split(",");	
		}else{
			accessAdress = new String[]{};
		}
		

		Map<String, Object> returnMap = new HashMap<String, Object>();
		if (dailyCheckDetail != null) {
			returnMap.put("dailyCheckDetail", dailyCheckDetail);
			returnMap.put("accessAdress", accessAdress);
			response.setData(returnMap);
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("数据返回成功!");
		}
		return response;
	}

	/**
	 * <p>Title: deleteDailyCheck</p>
	 * <p>Description: 删除日常巡检</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.dailyCheck.service.IDailyCheckService#deleteDailyCheck(com.fire.control.system.support.protocol.dailyCheck.DailyCheckRequestProtocol)
	 */
	@Override
	public ResponseProtocol deleteDailyCheck(DailyCheckRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		return null;
	}

	/**
	 * <p>Title: exportDailyCheck</p>
	 * <p>Description: 导出日常巡检</p>
	 * @param requestProtocol
	 * @return
	 * @see com.fire.control.system.module.dailyCheck.service.IDailyCheckService#exportDailyCheck(com.fire.control.system.support.protocol.dailyCheck.DailyCheckRequestProtocol)
	 */
	@Override
	public ResponseProtocol exportDailyCheck(DailyCheckRequestProtocol requestProtocol) {
		// TODO Auto-generated method stub
		return null;
	}

}
